package org.zwy.gradetrack.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.InternshipUnit;
import org.zwy.gradetrack.service.InternshipUnitService;

import java.util.List;

/**
 * 实习单位控制器
 */
@RestController
@RequestMapping("/api/internship-units")
public class InternshipUnitController {

    @Autowired
    private InternshipUnitService internshipUnitService;

    /**
     * 分页查询实习单位列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<Page<InternshipUnit>>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name) {
        Page<InternshipUnit> page = new Page<>(current, size);
        return ResponseEntity.ok(Result.success(internshipUnitService.pageInternshipUnits(page, name)));
    }

    /**
     * 获取所有实习单位
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<List<InternshipUnit>>> list() {
        return ResponseEntity.ok(Result.success(internshipUnitService.list()));
    }

    /**
     * 创建实习单位
     */
    @PostMapping
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> create(@RequestBody InternshipUnit internshipUnit) {
        try {
            return ResponseEntity.ok(Result.success(internshipUnitService.createInternshipUnit(internshipUnit)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 更新实习单位
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> update(@PathVariable Long id, @RequestBody InternshipUnit internshipUnit) {
        try {
            internshipUnit.setId(id);
            return ResponseEntity.ok(Result.success(internshipUnitService.updateInternshipUnit(internshipUnit)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 删除实习单位
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> delete(@PathVariable Long id) {
        try {
            return ResponseEntity.ok(Result.success(internshipUnitService.deleteInternshipUnit(id)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 获取实习单位详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<InternshipUnit>> getById(@PathVariable Long id) {
        try {
            return ResponseEntity.ok(Result.success(internshipUnitService.getInternshipUnitById(id)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(404, e.getMessage()));
        }
    }

    /**
     * 根据名称查询实习单位
     */
    @GetMapping("/search/name")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<List<InternshipUnit>>> getByName(@RequestParam String name) {
        return ResponseEntity.ok(Result.success(internshipUnitService.getInternshipUnitsByName(name)));
    }

    /**
     * 批量导入实习单位
     */
    @PostMapping("/batch")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> batchImport(@RequestBody List<InternshipUnit> internshipUnits) {
        try {
            return ResponseEntity.ok(Result.success(internshipUnitService.batchImport(internshipUnits)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }
} 